package com.quick.develop.flowable.service.task.impl;

import cn.hutool.core.collection.CollUtil;
import com.quick.develop.flowable.framework.factory.FlowServiceFactory;
import com.quick.develop.flowable.mapper.BpmVariableMapper;
import com.quick.develop.flowable.service.task.IBpmVariableService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Set;

/**
 * desc:
 *
 * @author junyuan.liu
 * @date 2023/6/19 14:56
 */
@Component
@Slf4j
public class BpmVariableServiceImpl extends FlowServiceFactory implements IBpmVariableService {
    @Resource
    BpmVariableMapper bpmVariableMapper;

    @Override
    public Integer getVarCount(String procInstId, Set<String> names) {
        return bpmVariableMapper.getVarCount(procInstId, names);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deleteHisVar(String procInstId, Set<String> names) {
        List<String> list = bpmVariableMapper.getAssigneeBytearry(procInstId);
        if (CollUtil.isNotEmpty(list)) {
            bpmVariableMapper.deleteAssigneeBytearry(list);
        }
        Integer count = this.getVarCount(procInstId, names);
        if (count > 0) {
            bpmVariableMapper.deleteVarByNames(procInstId, names);

        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteHisNotNeedVar(String procInstId, Set<String> names) {
        List<String> list = bpmVariableMapper.getAssigneeBytearry(procInstId);
        if (CollUtil.isNotEmpty(list)) {
            bpmVariableMapper.deleteAssigneeBytearry(list);
        }
        Integer count = bpmVariableMapper.getNeddDelVarCount(procInstId, names);
        if (count > 0) {
            bpmVariableMapper.deleteNotNeedVar(procInstId, names);
        }

    }
}
